package app

import (
	"data_summary/contrab/job"
	"errors"
	"golang.org/x/net/context"
	"sync"
)

type Result struct {
	output string
	err    error
}

// 执行shell命令，可设置执行超时时间
func ExecShell(ctx context.Context, command string) (string, error) {
	wg := sync.WaitGroup{}
	output := ""
	var err = errors.New(command)
	switch command {
	case "销售汇总":
		jobDo := job.NewOrderStat(&wg)
		output, err = jobDo.Run(ctx) //此处执行job
	}

	resultChan := make(chan Result)
	resultChan <- Result{string(output), err}
	// 等待所有登记的goroutine都结束
	select {
	case <-ctx.Done():
		return "", errors.New("timeout killed")
	case result := <-resultChan:
		return result.output, result.err
	}
}
